package it.azienda.managerworkstandard.view;

import it.azienda.framework.utilita.Utilita;
import it.azienda.managerworkstandard.modello.BeanCliente;
import it.azienda.managerworkstandard.modello.BeanLogin;
import java.util.ArrayList;
import javax.faces.component.html.HtmlForm;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import org.apache.myfaces.component.html.ext.HtmlInputHidden;
import org.apache.myfaces.custom.tabbedpane.HtmlPanelTab;
import org.apache.myfaces.custom.tabbedpane.HtmlPanelTabbedPane;
import org.apache.myfaces.custom.tabbedpane.TabChangeEvent;
import org.apache.myfaces.custom.tabbedpane.TabChangeListener;

public class TabCliente implements TabChangeListener {

    private static final long serialVersionUID = 1L;
    int selectedTabIndex;

    public TabCliente() {
        super();
    }

    public int getSelectedTabIndex() {
        return selectedTabIndex;
    }

    public void setSelectedTabIndex(int selectedTabIndex) {
        this.selectedTabIndex = selectedTabIndex;
    }

    public void processTabChange(TabChangeEvent event) throws AbortProcessingException {
        event.getNewTabIndex();
        this.selectedTabIndex = event.getNewTabIndex();
        FacesContext ctx = FacesContext.getCurrentInstance();
        if (event.getOldTabIndex() != event.getNewTabIndex()) {
            HtmlPanelTab panelTab1 = null;

            if (this.getSelectedTabIndex() == 0) {
                ctx = FacesContext.getCurrentInstance();
                BeanCliente beanCliente = (BeanCliente) ctx.getApplication().getVariableResolver().resolveVariable(ctx, "beanCliente");
                Utilita.getRequest().setAttribute("beanCliente", beanCliente);
                BeanCliente cliente = (BeanCliente) Utilita.getRequest().getAttribute("beanCliente");

                HtmlPanelTabbedPane tabbedPane = (HtmlPanelTabbedPane) ctx.getViewRoot().findComponent("panelTabbedPane");
                panelTab1 = (HtmlPanelTab) tabbedPane.findComponent("tab0");

                HtmlPanelTab panelTab11 = (HtmlPanelTab) tabbedPane.findComponent("tab11");
                HtmlPanelTab panelTab12 = (HtmlPanelTab) tabbedPane.findComponent("tab13");
                HtmlPanelTab panelTab3Cli = (HtmlPanelTab) tabbedPane.findComponent("tab3Cli");
                HtmlPanelTab panelTab14 = (HtmlPanelTab) tabbedPane.findComponent("tab14");
                
                panelTab12.setRendered(true);
                panelTab3Cli.setRendered(true);
                panelTab11.setRendered(true);
                panelTab14.setRendered(true);

                BeanLogin login = (BeanLogin) Utilita.getSession().getAttribute("login");
                login.settaHashMapStorico(new ArrayList());
                login.settaHashMapStorico2(new ArrayList());
                Utilita.getSession().removeAttribute("login");
                Utilita.getSession().setAttribute("login", login);
                return;
            } else {
                HtmlPanelTabbedPane tabbedPane = (HtmlPanelTabbedPane) ctx.getViewRoot().findComponent("panelTabbedPane");
                panelTab1 = (HtmlPanelTab) tabbedPane.findComponent("tab0");

                HtmlPanelTab panelTab11 = (HtmlPanelTab) tabbedPane.findComponent("tab11");
                HtmlPanelTab panelTab12 = (HtmlPanelTab) tabbedPane.findComponent("tab13");
                HtmlPanelTab panelTab3Cli = (HtmlPanelTab) tabbedPane.findComponent("tab3Cli");
                HtmlPanelTab panelTab14 = (HtmlPanelTab) tabbedPane.findComponent("tab14");

                HtmlForm form1 = (HtmlForm) panelTab1.findComponent("formLookup");
                HtmlInputHidden idCliente = (HtmlInputHidden) form1.findComponent("idCliente");
                String gg = "";
                if (idCliente.getValue() != null) {
                    gg = idCliente.getValue().toString();
                } else {
                    return;
                }
                DettaglioCliente dettaglio = new DettaglioCliente();
                Integer tab = new Integer(selectedTabIndex);

                if (tab.intValue() == 3) {
                    panelTab11.setRendered(false);
                    panelTab14.setRendered(false);
                    panelTab3Cli.setRendered(false);
                    panelTab12.setRendered(true);

                } else if (tab.intValue() == 1) {
                    panelTab12.setRendered(false);
                    panelTab14.setRendered(false);
                    panelTab3Cli.setRendered(false);
                    panelTab11.setRendered(true);

                }else if (tab.intValue() == 4) {
                    panelTab12.setRendered(false);
                    panelTab14.setRendered(true);
                    panelTab3Cli.setRendered(false);
                    panelTab11.setRendered(false);

                } else {
                    panelTab3Cli.setRendered(true);
                    panelTab11.setRendered(false);
                    panelTab12.setRendered(false);
                    panelTab14.setRendered(false);
                }

                dettaglio.caricaStoricoCliente(new Integer(gg), tab);
                return;
            }
        }
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }
}